import 'package:flutter/foundation.dart';
import 'package:loxen/api/api.dart';
import 'package:loxen/mode/material/material_order_mode.dart';
import 'package:rxdart/rxdart.dart';

mixin MaterialOrderMixin {
  ///删除订单样单  200
  Stream<int> deleteShopcar(String pks) {
    return ApiService.deleteShopcar(pks).flatMap((value) => Stream.value(value.statusCode));
  }

  ///样单数量
  Stream<int> shiocarNum() {
    return ApiService.shopcarNum().flatMap((value) {
      int num = 0;
      if (value.data is Map) {
        var data = value.data['data']['count'];
        if (data is int) {
          num = data;
        }
      }
      return Stream.value(num);
    });
  }

  //添加样单
  Stream<Map> shopcar(String pk) {
    return ApiService.shopcar(pk).flatMap((event) {
      return Stream.value(event.data);
    });
  }

  //样单列表
  Stream<List<MaterialOrder>> shopcarList() {
    return ApiService.shopcarList()
        .flatMap((value) => compute(shopcarListDecode, value.data).asStream())
        .flatMap((value) => Stream.value(value?.data));
  }

  static Root shopcarListDecode(dynamic jsonRes) {
    return Root.fromJson(jsonRes);
  }
}
